﻿using NPOI.SS.Util;
using OSSUtility.Communicate;
using OSSUtility.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ozone.Device
{
    /// <summary>
    /// 三工位设备  旋转夹爪 开瓶盖 关瓶盖使用
    /// </summary>
    public class DevThreeStationSpinClaw : DevBase
    {
        public DevThreeStationSpinClaw(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {

        }
        public bool OpenOrCloseCla11w(float journey)
        {

            bool temperature = false;
            if (journey > 20 || journey < 0)
            {
                return temperature;
            }
            byte[] array1 = BitConverter.GetBytes(journey);
            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //寄存器地址
                ushort startAddr = 0x08EC;
                //发送数据
                byte[] array2 = new byte[] {
                    0x04,0x02,0x00
                };
                byte[] floatBytes = new byte[array1.Length + array2.Length];
                Array.Copy(array1, 0, floatBytes, 0, array1.Length);
                Array.Copy(array2, 0, floatBytes, array1.Length, array2.Length);

                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }



        /// <summary>
        /// 初始化夹爪   0 正常初始化 其他异常
        /// </summary>
        /// <returns></returns>
        public string InitClaw()
        {

            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //寄存器地址
                ushort startAddr = 0x00;
                //发送数据
                byte[] floatBytes = new byte[] {
                    0x01,0x00
                };
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Count() == 8)
                {
                    if (byteRcv[4] == 0x00)
                    {
                        return "0";
                    }
                    else if (byteRcv[4] == 0x01)
                    {
                        return "1";
                    }
                    else if (byteRcv[4] == 0x02)
                    {
                        return "2";
                    }
                    else if (byteRcv[4] == 0x03)
                    {
                        return "3";
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }



            return "";

        }


        /// <summary>
        /// 打开或者关闭夹爪
        /// 单位 mm，取值范围：0~20  由于结构件阻挡行程只能 10- 20
        /// 打开夹爪20 
        /// 关闭夹爪 0
        /// </summary>
        /// <returns></returns>
        public bool OpenOrCloseClaw(float journey)
        {

            bool temperature = false;
            if (journey > 20 || journey < 0)
            {
                return temperature;
            }
            byte[] array1 = BitConverter.GetBytes(journey);
            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //寄存器地址
                ushort startAddr = 0x02;
                //发送数据
                byte[] array2 = new byte[] {
                    0x04,0x02,0x00
                };
                byte[] floatBytes = new byte[array1.Length + array2.Length];
                Array.Copy(array1, 0, floatBytes, 0, array1.Length);
                Array.Copy(array2, 0, floatBytes, array1.Length, array2.Length);

                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {

                    temperature = true;
                    //int Number = 0;
                    //while (true)
                    //{
                    //    Thread.Sleep(1000);
                    //    //夹爪到位
                    //    if (GetClawState() == "0")
                    //    {
                    //        temperature = true;
                    //        break;
                    //    }
                    //    if (Number > 20)
                    //    {
                    //        temperature = false;
                    //        break;
                    //    }
                    //    Number++;
                    //}
                }
            }
            catch (Exception ex)
            {
                temperature = false;
            }
            return temperature;

        }

        /// <summary>
        /// 旋转夹爪
        /// 单位 度，取值范围：-3600000~3600000
        /// </summary>
        /// <param name="Point"></param>
        /// <returns></returns>
        public bool SpinClaw(float Point)
        {
            bool temperature = false;
            byte[] array1 = BitConverter.GetBytes(Point);

            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //寄存器地址
                ushort startAddr = 0x0A;
                //发送数据
                byte[] array2 = new byte[] {
                    0x04,0x02,0x00
                };
                byte[] floatBytes = new byte[array1.Length + array2.Length];
                Array.Copy(array1, 0, floatBytes, 0, array1.Length);
                Array.Copy(array2, 0, floatBytes, array1.Length, array2.Length);

                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    int Number = 0;

                    while (true)
                    {
                        Thread.Sleep(1000);
                        //旋转到位
                        if (GetSpinClawState() == "0")
                        {
                            temperature = true;
                            break;
                        }
                        if (Number > 20)
                        {
                            temperature = false;
                            break;
                        }
                        Number++;
                    }
                }
            }
            catch (Exception ex)
            {
                temperature = false;
                throw;
            }
            return temperature;

        }

        /// <summary>
        /// 设置夹爪旋转速度
        /// 单位 度/s，取值范围：1~1080
        /// </summary>
        /// <param name="Speed"></param>
        /// <returns></returns>
        public bool SetSpinSpeedClaw(float Speed)
        {

            bool temperature = false;


            byte[] array1 = BitConverter.GetBytes(Speed);

            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x10;
                //寄存器地址
                ushort startAddr = 0x0E;
                //发送数据
                byte[] array2 = new byte[] {
                    0x04,0x02,0x00
                };
                byte[] floatBytes = new byte[array1.Length + array2.Length];
                Array.Copy(array1, 0, floatBytes, 0, array1.Length);
                Array.Copy(array2, 0, floatBytes, array1.Length, array2.Length);

                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Length == 8)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            GetSpinSpeedClaw();

            return temperature;

        }

        /// <summary>
        /// 获取夹爪旋转速度
        /// 单位 度/s，取值范围：1~1080
        /// </summary>
        /// <param name="Speed"></param>
        /// <returns></returns>
        public string GetSpinSpeedClaw()
        {

            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x03;
                //寄存器地址
                ushort startAddr = 0x4C;
                //发送数据
                byte[] floatBytes = new byte[] {
                    0x01,0x00
                };
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray(), 1000);
                if (byteRcv != null && byteRcv.Count() == 7)
                {
                    if (byteRcv[4] == 0x00)
                    {
                        return "0";
                    }
                    else if (byteRcv[4] == 0x01)
                    {
                        return "1";
                    }
                    else if (byteRcv[4] == 0x02)
                    {
                        return "2";
                    }
                    else if (byteRcv[4] == 0x03)
                    {
                        return "3";
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }



            return "";

        }


        /// <summary>
        /// 获取夹爪开合状态
        /// 00 00表示到位，0：到位，1：运动中，2：夹持，3：掉落）
        /// </summary>
        /// <returns></returns>
        public string GetClawState()
        {


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x03;
                //寄存器地址
                ushort startAddr = 0x41;
                //发送数据
                byte[] floatBytes = new byte[] {
                    0x01,0x00
                };
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Count() == 7)
                {
                    if (byteRcv[4] == 0x00)
                    {
                        return "0";
                    }
                    else if (byteRcv[4] == 0x01)
                    {
                        return "1";
                    }
                    else if (byteRcv[4] == 0x02)
                    {
                        return "2";
                    }
                    else if (byteRcv[4] == 0x03)
                    {
                        return "3";
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return "";

        }

        /// <summary>
        /// 获取夹爪旋转状态
        /// 0：到位，1：旋转中，2：旋转受阻，3：掉落，4：堵转停转
        /// </summary>
        /// <returns></returns>
        public string GetSpinClawState()
        {
            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x03;
                //寄存器地址
                ushort startAddr = 0x48;
                //发送数据
                byte[] floatBytes = new byte[] {
                    0x01,0x00
                };
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, floatBytes);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null && byteRcv.Count() == 7)
                {
                    if (byteRcv[4] == 0x00)
                    {
                        return "0";
                    }
                    else if (byteRcv[4] == 0x01)
                    {
                        return "1";
                    }
                    else if (byteRcv[4] == 0x02)
                    {
                        return "2";
                    }
                    else if (byteRcv[4] == 0x03)
                    {
                        return "3";
                    }
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return "";

        }

        /// <summary>
        /// 卸瓶盖
        /// </summary>
        /// <returns></returns>
        public bool UnInstallbottlecap()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }





        /// <summary>
        /// 补液
        /// </summary>
        /// <returns></returns>
        public bool FluidInfusion()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }


        /// <summary>
        /// 获取三工位运行状态  
        /// true  运行中 
        /// false  停止状态
        /// </summary>
        /// <returns></returns>
        public bool GetThreeStationRunState()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }


        /// <summary>
        /// 馏分清洗
        /// </summary>
        /// <returns></returns>
        public bool CollectCleaning()
        {

            bool temperature = false;


            try
            {
                //站点
                byte stationid = 0x01;
                //功能码
                byte funcode = 0x06;
                //起始地址
                ushort startAddr = 0x01;
                //发送数据
                byte[] bytesData = new byte[2];
                bytesData[1] = 0x00;
                bytesData[0] = 0x01;
                List<byte> returnbytes = Modbus.writeFreameRTUSingle(stationid, funcode, startAddr, bytesData);
                byte[] byteRcv = session_.sendAndReadByteLock(returnbytes.ToArray());
                if (byteRcv != null)
                {
                    temperature = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }

            return temperature;

        }




    }
}
